// 标签系统相关的mutations
export default {
  // 设置标签列表
  SET_TAGS(state, tags) {
    state.tags = tags
  },
  
  // 设置包的标签
  SET_PACKAGE_TAGS(state, { packageName, tags }) {
    state.currentPackageName = packageName
    state.packageTags = tags
  },
  
  // 设置加载状态
  SET_LOADING(state, loading) {
    state.loading = loading
  },
  
  // 设置保存状态
  SET_SAVING(state, saving) {
    state.saving = saving
  },
  
  // 设置删除状态
  SET_DELETING(state, deleting) {
    state.deleting = deleting
  },
  
  // 设置错误信息
  SET_ERROR(state, error) {
    state.error = error
  },
  
  // 添加新标签
  ADD_TAG(state, tag) {
    state.tags.push(tag)
  },
  
  // 更新标签
  UPDATE_TAG(state, updatedTag) {
    const index = state.tags.findIndex(tag => tag.id === updatedTag.id)
    if (index !== -1) {
      state.tags.splice(index, 1, updatedTag)
    }
    
    // 如果该标签已被分配给当前包，也更新包标签
    const packageTagIndex = state.packageTags.findIndex(tag => tag.id === updatedTag.id)
    if (packageTagIndex !== -1) {
      state.packageTags.splice(packageTagIndex, 1, updatedTag)
    }
  },
  
  // 删除标签
  DELETE_TAG(state, tagId) {
    state.tags = state.tags.filter(tag => tag.id !== tagId)
    
    // 如果该标签已被分配给当前包，也从包标签中删除
    state.packageTags = state.packageTags.filter(tag => tag.id !== tagId)
  },
  
  // 为包添加标签
  ADD_PACKAGE_TAG(state, tag) {
    state.packageTags.push(tag)
  },
  
  // 从包中移除标签
  REMOVE_PACKAGE_TAG(state, tagId) {
    state.packageTags = state.packageTags.filter(tag => tag.id !== tagId)
  },
  
  // 清空包标签
  CLEAR_PACKAGE_TAGS(state) {
    state.packageTags = []
    state.currentPackageName = ''
  },
  
  // 清空所有标签
  CLEAR_ALL_TAGS(state) {
    state.tags = []
    state.packageTags = []
    state.currentPackageName = ''
  }
}